﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.member;
using umbraco.BusinessLogic;
using TheOutfield.UmbExt.PrivateMessaging.Services;

namespace TheOutfield.UmbExt.PrivateMessagingDemo.UserControls
{
    public partial class PrivateMessagingDemoInstaller : System.Web.UI.UserControl
    {
        protected MemberType _memberType;
        protected MemberGroup _memberGroup;
        protected Member _member1;
        protected Member _member2;

        protected void Page_Load(object sender, EventArgs e)
        {
            CreateMemberType();
            CreateMemberGroup();
            CreateMembers();
            CreateMessages();
        }

        private void CreateMessages()
        {
            var pmService = new PrivateMessageService();

            pmService.SendPrivateMessage(_member2.Id,
                new [] { _member1.LoginName },
                "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
                "Etiam neque felis, dictum in ornare quis, malesuada vitae mi. Nunc dignissim dolor sit amet nibh sodales eget blandit mi fermentum. Sed ut eros ac sapien pretium pulvinar quis at eros. Phasellus mauris diam, viverra et lobortis non, malesuada at mi. Nullam molestie rhoncus nibh in porta. Praesent dolor dui, varius id mattis ultricies, tincidunt sed velit. Maecenas interdum dignissim tellus vel sagittis. Cras id lacus et ipsum fermentum euismod id vel nibh. Donec rhoncus commodo venenatis.");

            pmService.SendPrivateMessage(_member2.Id,
                new [] { _member1.LoginName },
                "Nullam ut est erat vestibulum vel augue nisi",
                "Maecenas hendrerit aliquet fermentum. Phasellus sagittis molestie ipsum. Donec accumsan diam vel risus semper consectetur. Maecenas tempor dui at ligula fermentum eget pretium urna scelerisque. Maecenas viverra tempus ante id posuere. Suspendisse quis lorem nibh. Suspendisse potenti. Sed semper, turpis a fringilla porta, odio urna hendrerit augue, non tempor metus est et dui.");
            
            pmService.SendPrivateMessage(_member1.Id,
                   new string[] { _member2.LoginName },
                   "Aenean faucibus sollicitudin justo",
                   "Etiam accumsan condimentum molestie. Praesent ullamcorper eros vitae diam imperdiet molestie. Phasellus blandit, neque vel varius tempor, diam nulla semper nulla, non vulputate ante purus id neque. Aenean libero justo, accumsan at tristique et, vulputate quis dolor. In ornare, turpis ac consequat condimentum, libero leo tincidunt purus, eget lacinia est tortor sit amet justo. Maecenas semper, erat id commodo congue, diam mauris tempus risus, sed mollis enim augue eget odio. Vivamus quis odio a leo dapibus sollicitudin. In id nunc id erat vulputate adipiscing. Duis nunc sem, facilisis nec rhoncus a, condimentum ut dolor.");
        }

        private void CreateMembers()
        {
            _member1 = Member.GetMemberFromLoginName("PmMember1");

            if (_member1 == null)
            {
                _member1 = Member.MakeNew("PmMember1", _memberType, User.GetUser(0));
                _member1.Password = "PmMember1";
                _member1.Email = "pmmember1@test.com";
                _member1.AddGroup(_memberGroup.Id);
                _member1.Save();
            }

            _member2 = Member.GetMemberFromLoginName("PmMember2");

            if (_member2 == null)
            {
                _member2 = Member.MakeNew("PmMember2", _memberType, User.GetUser(0));
                _member2.Password = "PmMember2";
                _member2.Email = "pmmember2@test.com";
                _member2.AddGroup(_memberGroup.Id);
                _member2.Save();
            }
        }

        private void CreateMemberGroup()
        {
            _memberGroup = MemberGroup.GetByName("[PM] Member Group");

            if (_memberGroup == null)
            {
                _memberGroup = MemberGroup.MakeNew("[PM] Member Group", User.GetUser(0));
                _memberGroup.Save();
            }
        }

        private void CreateMemberType()
        {
            try
            {
                _memberType = MemberType.GetByAlias("pmMemberType");
            }
            catch(ArgumentException e)
            {
                _memberType = MemberType.MakeNew(User.GetUser(0), "pmMemberType");
                _memberType.Text = "[PM] Member Type";
                _memberType.Save();
            }
        }
    }
}